<?php
if (!defined('IS_ADMIN'))
	die('Error 404 : Not found.');

$msg = '';
$post = array();

// Get record
$Qry = 'SELECT *
		FROM games
		WHERE g_id = ' . (int)$id;
$row = $db->fetchAll($Qry);

// initial Preview
$initialPreview = '';
if ($row[0]['images']) {
	$img_path = DC_ROOT . '/data/uploads/' . $row[0]['images'];

	if (file_exists($img_path))
		$initialPreview = '/data/uploads/' . $row[0]['images'];
}

// Condition
$condition = 'g_id = ' . $id;

// Explode category
$current_cat = explode(',', strtolower($row[0]['category']));

// Old image file
$img = $_SERVER['DOCUMENT_ROOT'] . '/data/uploads/' . $row[0]['images'];
$old_img = $row[0]['images'];

// All categories
$Qry = 'SELECT c_id, c_name, c_url
		FROM categories
		WHERE c_active = 1
		ORDER BY c_name ASC';
$categories = $db->fetchAll($Qry);

// All tags
$Qry = 'SELECT name, tag
		FROM cat
		ORDER BY name ASC';
$all_tags = $db->fetchAll($Qry);

if ($submit) {
	$_return	= isset($_POST['return_url']) ? strval($_POST['return_url']) : NULL;

	$g_name		= isset($_POST['g_name']) ? strval($_POST['g_name']) : $row[0]['g_name'];
	$g_name		= cleaner($g_name, true);

	$g_url		= isset($_POST['g_url']) ? strval($_POST['g_url']) : $row[0]['g_url'];
	$g_url		= remove_unicode($g_url, true);

	if (!$g_url)
		$g_url	= remove_unicode($g_name, true);

	$category	= isset($_POST['category']) ? $_POST['category'] : $row[0]['category'];
	if ($category) {
		$category 	= implode(',', $category);
		$category	= cleaner($category, true);
	} else
		$post['error'] .= '<li>Category field is blank.</li>';

	$tags		= isset($_POST['tags']) ? strval($_POST['tags']) : $row[0]['tags'];
	if ($tags) {
		$tags = trim($tags);
		$tags = rtrim($tags, ',');
		$tags = explode(',', $tags);
		$tags = array_unique($tags);
		$tags = implode(',', $tags);
		$tags = cleaner($tags, true);
	}

	// Game embed
	$object		= isset($_POST['object']) ? strval($_POST['object']) : $row[0]['object'];
	$width		= isset($_POST['width']) ? intval($_POST['width']) : $row[0]['width'];
	$height		= isset($_POST['height']) ? intval($_POST['height']) : $row[0]['height'];

	// Summary && Description && How to play?
	$summary	= isset($_POST['summary']) ? strval($_POST['summary']) : $row[0]['summary'];
	$summary	= cleaner($summary, true);
	$smarty->assign('summary', $summary);

	$description	= isset($_POST['description']) ? strval($_POST['description']) : $row[0]['description'];
	$description	= cleaner($description, true);

	if (!$description)
		$post['error'] .= '<li>Description field is blank.</li>';

	$howto		= isset($_POST['howto']) ? strval($_POST['howto']) : $row[0]['howto'];
	$howto		= cleaner($howto, true);

// 	if (!$howto)
// 		$post['error'] .= '<li>How to field is blank.</li>';

	if (!empty($post['error'])) {
		$msg = '<strong>Warning</strong>! ' . $post['error'];
		$class = 'alert-warning';
		$script = '';
	} else {
		// Image
		$image_up_name = $_FILES['image_up']['name'];

		if (!empty($image_up_name)){
			$data_image = xupload($_FILES['image_up'], '../data/uploads/', $image_up_name);

			if (empty($data_image['error']))
				$image_up = $data_image['filename'];
			else
				$image_up = '';
		}

		if (!empty($data_image['error'])) {
			$msg = $data_image['error'];
			$class = 'alert-warning';
			$script = '';
		} else {
			if ($image_up != '' && $image_up != $old_img){
				// Delete old file
				if (file_exists($img))
					unlink($img);

				$dataImg = array(
						'images'=>$image_up
				);
				$db->update('games', $dataImg, $condition);
			}

			$data = array(
					'g_name'=>$g_name,
					'g_url'=>$g_url,

					'category'=>$category,
					'tags'=>$tags,

					'object'=>$object,
					'width'=>$width,
					'height'=>$height,

					'summary'=>$summary,
					'description'=>$description,
					'howto'=>$howto
			);
			$db->update('games', $data, $condition);

			$msg = '<strong>Success</strong>! The game has been saved. [Redirect after 3 seconds]';
			$class = 'alert-success';
			$script = "setTimeout(\"location.href='$_return';\", 3000);";
		}
	}
}

// assign
$smarty->assign('referer', $referer);
$smarty->assign('class', $class);
$smarty->assign('status', $msg);
$smarty->assign('script', $script);

$smarty->assign('initialPreview', $initialPreview);

$smarty->assign('row', $row);
$smarty->assign('current_cat', $current_cat);
$smarty->assign('categories', $categories);
$smarty->assign('all_tags', $all_tags);
// SEO
$smarty->assign('title', 'Edit Game - ' . $lang['home_title']);
// Menu
$smarty->assign('is_menu', 'games');
// display theme using smarty
$smarty->display(str_replace(SKIN, 'admin', SMARTY_TEMP) . 'games_edit.tpl');